#!/bin/bash
###########################################
#               README
# 1 attach the session : `tmux attach-session -t hdSysMonitor`
# 2 detach the session : <ctrl>+b d
# 3 kill the session   : `tmux kill-session -t hdSysMonitor`
# 3 kill all sessions  : `tmux kill-server`
#
###########################################


function showInterrupts {
    # Parse command-line arguments
    if [ $# -ne 1 ]; then
        echo "Usage: $1 <core_list>"
        exit 1
    fi

    core_list=$1

    # Header for output
    printf "%-4s" "Core"
    for core in $core_list
    do
        printf "%10s" "$core"
    done
    printf "\n"


    # Loop over each interrupt and get the counts for the specified cores
    interrupts=("TLB" "CAL" "LOC")
    for interrupt in "${interrupts[@]}"
    do
        printf "%-4s" "$interrupt"
        line=$(cat /proc/interrupts | grep "$interrupt")
        read -ra items <<< "$line"
        for core in $core_list
        do  
        ¦   printf "%10s" "${items[$core+1]}"
        done
        printf "\n"
    done
}



# Create a new tmux session named hdSysMonitor
tmux new-session -d -s hdSysMonitor

# Split the window into 4 panes
tmux split-window -h -p 50
tmux split-window -v -p 33
tmux split-window -v -p 50
tmux select-pane -t 0

# Run htop in the left pane
tmux send-keys "htop" C-m

# Run 3 different bash commands in the right panes from top to bottom
tmux select-pane -t 1
tmux send-keys 'watch -n.5 "cat /proc/interrupts | grep -E \"(LOC|TLB|CAL)\"" ' C-m
tmux select-pane -t 2
tmux send-keys "watch -n.5 'cat /proc/cpuinfo | grep MHz'" C-m
tmux select-pane -t 3
core_list="\"1 2 3 4 5 6 7\""
tmux send-keys "watch -n.5 'bash printInterrupts.sh $core_list'" C-m


# Attach to the session hdSysMonitor
tmux attach-session -t hdSysMonitor



